﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XCommunity.Caching;

namespace XCommunity.Members
{
  public class MemberCache : CacheContainer
  {


    private MemberService _service;

    public MemberCache( MemberService service )
    {
      _service = service;
    }

    protected MemberService MemberService
    {
      get { return _service; }
    }

    public void AddMember( Member member )
    {
      AddItem( member.Username, member );
    }

    public Member GetMember( string username )
    {
      return TryGetValue( username ) as Member;
    }


    private long longTimeTicks = new TimeSpan( 0, 30, 0 ).Ticks;
    private long shortTimeTicks = new TimeSpan( 0, 1, 0 ).Ticks;

    public override bool CheckAvailable( CacheItem cacheItem )
    {
      DateTime now = Service.GetTime();
      DateTime time = now - new TimeSpan( 0, 5, 0 );

      string username = (string) cacheItem.Key;
      if ( MemberService.AccountService.GetLastActivityTime( username ) > time )
        return now.Ticks <= cacheItem.RefreshTime.Ticks + shortTimeTicks;
      else
        return now.Ticks <= cacheItem.RefreshTime.Ticks + longTimeTicks;
    }
  }
}
